package com.jinmdz.fmis.dao.dao.provider;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

/**
 * 殡仪馆业务总览
 *
 * @author GuanKui
 * @date 2020/2/28 9:06
 */
public class SearchOverviewProvider {

    /**
     * 查询冷柜冷藏情况
     *
     * @param outState 出藏状态
     * @return
     * @author GuanKui
     * @date 2020/2/27  15:26
     */
    public String getOverviewEquipmentCount(@Param("outState") Integer outState) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" COUNT(*) as equipment_count");
                builder.append(" ,SUM(case when disable_state = 0 then 0 else 1 end) as disable_equipmentCount ");
                builder.append(" ,( select COUNT(DISTINCT equipment_id) from cold_storage where cold_storage.deleted = 0 ");
                builder.append("    and process_state_code != #{outState}) as in_equipment_count");
                SELECT(builder.toString());
                FROM(" cold_storage_equipment ");
                WHERE(" deleted = 0 ");
            }
        }.toString();
    }

    /**
     * 查询礼厅情况
     *
     * @param outState 出厅状态
     * @param inState  入厅状态
     * @return
     * @author GuanKui
     * @date 2020/2/28  9:09
     */
    public String getOverviewHallCount(@Param("outState") Integer outState, @Param("inState") Integer inState) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" COUNT(*) as hall_count ");

                builder.append(" ,( select COUNT(DISTINCT hall_id) from mourn_ceremony where deleted = 0 ");
                builder.append("    and process_state_code != #{outState} and appointment_in_time is not null ");
                // 判断条件
                StringBuilder whereExt = new StringBuilder();
                // 原预约出厅时间为空
                whereExt.append(" appointment_out_time is null ");
                // 原流程状态值
                whereExt.append(" or process_state_code = #{inState}");
                // 原预约入厅时间小于当前时间，原预约出厅时间大于当前时间
                whereExt.append(" or ( appointment_in_time <= now() and appointment_out_time >= now())");
                builder.append(" and (" + whereExt.toString() + " )) as in_hall_count");

                SELECT(builder.toString());
                FROM(" mourn_ceremony_hall ");
                WHERE(" deleted = 0 ");
            }
        }.toString();
    }
}
